package cn.school.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.school.common.constant.CommonConstant;
import cn.school.common.utils.SysUserUtils;
import cn.school.sys.dao.TermTimeDao;
import cn.school.sys.entity.TermTime;
import cn.school.sys.service.TermTimeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Slf4j
@Service("termTimeService")
public class TermTimeServiceImpl extends ServiceImpl<TermTimeDao, TermTime> implements TermTimeService {
    @Override
    public IPage<TermTime> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");

        LambdaQueryWrapper<TermTime> wrapper = new LambdaQueryWrapper<>();
    
    
        if (StrUtil.isNotEmpty(beginTime)) {
            wrapper.ge(TermTime::getCreateTime, DateUtil.parse(beginTime + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
        }
        if (StrUtil.isNotEmpty(endTime)) {
            wrapper.le(TermTime::getCreateTime, DateUtil.parse(endTime + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        if (Objects.isNull(tenantId)) {
            tenantId = Objects.isNull(SysUserUtils.getParentUser().getTenantId()) ? SysUserUtils.getStudentUser().getTenantId() : SysUserUtils.getParentUser().getTenantId();
        }
        //添加租户条件
        wrapper.eq(TermTime::getTenantId, tenantId);
        wrapper.orderByDesc(TermTime::getCreateTime);

        wrapper.apply(params.get(CommonConstant.SQL_FILTER) != null, (String) params.get(CommonConstant.SQL_FILTER));
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public TermTime getTermTimeById(Long id) {
        TermTime termTime = this.baseMapper.selectById(id);
        return termTime;
    }

    @Override
    public int update(Long id, TermTime termTime) {
        termTime.setId(id);
        return this.baseMapper.updateById(termTime);
    }

    @Override
    public int create(TermTime termTime) {
        return this.baseMapper.insert(termTime);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<TermTime> getListByAcyeId(Short beginYear, Short endYear) {
        LambdaQueryWrapper<TermTime> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TermTime::getTetiBeginYear, beginYear);
        wrapper.eq(TermTime::getTetiEndYear, endYear);
        wrapper.orderByAsc(TermTime::getTetiTerm);
        List<TermTime> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() != 0) {
            Date sysDate = Calendar.getInstance().getTime();
            for (TermTime termTime : list) {
                if (termTime.getTetiTermBeginDate().getTime() <= sysDate.getTime() && sysDate.getTime() <= termTime.getTetiTermEndDate().getTime()) {
                    // 为当前时间，对应的学期
                    termTime.setCurrent(true); // 当前学期
                    break;
                }
            }
        }
        return list;
    }

    @Override
    public List<TermTime> getTermTimeByDate() {
        LambdaQueryWrapper<TermTime> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(TermTime::getTetiTermBeginDate, new Date());
        wrapper.ge(TermTime::getTetiTermEndDate, new Date());
        String tenantId="0";
        if(!StringUtils.isEmpty(SysUserUtils.getSysUser().getTenantId())){
            tenantId = SysUserUtils.getSysUser().getTenantId();
        }
        if(!StringUtils.isEmpty(SysUserUtils.getStudentUser().getTenantId())){
            tenantId = SysUserUtils.getStudentUser().getTenantId();
        }
        if(!StringUtils.isEmpty(SysUserUtils.getParentUser().getTenantId())){
            tenantId = SysUserUtils.getParentUser().getTenantId();
        }
        wrapper.eq(TermTime::getTenantId, tenantId);
        return this.baseMapper.selectList(wrapper);
    }
}
